package com.base.home.fag;

import android.annotation.SuppressLint;
import android.support.v7.widget.GridLayoutManager;
import android.view.View;

import com.alibaba.android.arouter.exception.HandlerException;
import com.alibaba.android.arouter.launcher.ARouter;
import com.base.commonlib.base.BaseApp;
import com.base.commonlib.base.CBaseMvpFragment;
import com.base.commonlib.bean.UpdateScopeStatEntity;
import com.base.commonlib.recy.HFGridMultiTypeGapDecoration;
import com.base.commonlib.recy.HFMultiTypeRecyAdapter;
import com.base.commonlib.recy.animation.ScaleInAnimation;
import com.base.commonlib.resh.api.RefreshLayout;
import com.base.commonlib.resh.footer.ClassicsFooter;
import com.base.commonlib.resh.header.ClassicsHeader;
import com.base.commonlib.resh.listener.OnRefreshLoadmoreListener;
import com.base.commonlib.sp.SpUtil;
import com.base.commonlib.utils.RxBus2;
import com.base.commonlib.utils.ToastUtil;
import com.base.home.R;
import com.base.home.adapter.HFGridMuiltType;
import com.base.home.adapter.MainData;
import com.base.home.adapter.WebBannerAdapter;
import com.base.home.banner.BannerLayout;
import com.base.home.bean.ConcertSelectEntity;
import com.base.home.bean.Score;
import com.base.home.databinding.FagConcertBinding;
import com.base.home.presenter.ConcertPresenter;
import com.base.home.view.ConcertView;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * 首页精选
 * Created by Administrator on 2020/2/13.
 */

@SuppressLint("ValidFragment")
public class ConcertFragment extends CBaseMvpFragment<ConcertPresenter> implements ConcertView{

    private FagConcertBinding mBinding;

    private HFMultiTypeRecyAdapter mAdapter;
    private String id;
    private int page = 2;
    Disposable disposable;

    @Override
    public void init() {
        mBinding = (FagConcertBinding) mRootBinding;
        mPresenter = new ConcertPresenter(this,_mActivity);
        mPresenter.start();
    }
//    @SuppressLint("ValidFragment")
//    public ConcertFragment(String id){
//        this.id = id;
//    }
//    public static ConcertFragment newInstance(String resourId) {
//        return new ConcertFragment(resourId);
//    }



    @Override
    public void start() {

        mBinding.refreshLayout.setRefreshHeader(new ClassicsHeader(_mActivity));
        mBinding.refreshLayout.setHeaderHeight(50);
        mBinding.refreshLayout.setRefreshFooter(new ClassicsFooter(_mActivity));
        mBinding.refreshLayout.setFooterHeight(60);
        if (mAdapter == null) {
            mAdapter = new HFMultiTypeRecyAdapter();
            mAdapter.openLoadAnimation(new ScaleInAnimation());
            mBinding.recycle.setAdapter(mAdapter);
            GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 12);
            HFGridMuiltType spanSizeLookup = new HFGridMuiltType();
            spanSizeLookup.setAdapter(mAdapter);
            spanSizeLookup.setLayoutManager(gridLayoutManager);
            gridLayoutManager.setSpanSizeLookup(spanSizeLookup);
            mBinding.recycle.setLayoutManager(gridLayoutManager);
            HFGridMultiTypeGapDecoration gapDecoration = new HFGridMultiTypeGapDecoration(0);
            gapDecoration.setOffsetTopEnabled(false);
            mBinding.recycle.addItemDecoration(gapDecoration);
        }
        mBinding.refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                //page++;
                mBinding.refreshLayout.finishLoadmore();
                mBinding.refreshLayout.setLoadmoreFinished(true);
               // mPresenter.initData(id,page);
//                mPresenter.loadMore(page);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                if (getArguments() != null) {
                    id = getArguments().getString("id");
                }
                mPresenter.initData(id);
            }
        });


        disposable = RxBus2.getInstance().doSubscribe(UpdateScopeStatEntity.class, new Consumer<UpdateScopeStatEntity>() {
            @Override
            public void accept(UpdateScopeStatEntity bean) throws Exception {
                page = 1;
                mPresenter.initData(id);
            }
        });
    }

    @Override
    protected void lazyLoad() {
        mBinding.refreshLayout.autoRefresh();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fag_concert;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPresenter.release();
        if (disposable!=null){
            disposable.dispose();
        }
    }

    WebBannerAdapter bannerAdapter;
    List<ConcertSelectEntity.BannersBean> imgs = new ArrayList<>();
    BannerLayout recyclerBanner;
    View mView;

    @Override
    public void returnHomeEntity(ConcertSelectEntity concertSelectEntity) {
        mBinding.refreshLayout.setLoadmoreFinished(false);
        mBinding.refreshLayout.finishRefresh();
        if (concertSelectEntity.getScore()!=-1) {
            Score score = new Score();
            score.setmScore(concertSelectEntity.getScore());
            score.setState(0X110);
            RxBus2.getInstance().post(score);
        }
        if (id.equals("0")) {
            if (concertSelectEntity.getBanners().size() != 0) {
                mView = getLayoutInflater().inflate(R.layout.item_top_banner, null);
                mAdapter.setHeadView(mView);
                recyclerBanner = mView.findViewById(R.id.recycler);
                imgs.clear();
                imgs.addAll(concertSelectEntity.getBanners());
                //轮播图处理
                bannerAdapter = new WebBannerAdapter(mContext, imgs);
                recyclerBanner.setAdapter(bannerAdapter);
                bannerAdapter.setOnBannerItemClickListener(new BannerLayout.OnBannerItemClickListener() {
                    @Override
                    public void onItemClick(int position) {
                        if (imgs.get(position).getWebUrl().startsWith("http")) {
                            ARouter.getInstance().build("/sweb/common")
                                    .withInt("key", 4)
                                    .withString("url", imgs.get(position).getWebUrl())
                                    .withString("title", "江苏中越科技")
                                    .withString("name", "测试")
                                    .navigation();
                        } else {
                            try {
                                ARouter.getInstance().build(imgs.get(position).getMClass()).withString("id", imgs.get(position).getId()).navigation();
                            } catch (HandlerException e) {
                                //ToastUtil.show("暂无权限");
                            }
                        }
                    }
                });
            }
            mAdapter.refreshDatas(MainData.getMainFragmentDatas(concertSelectEntity,true,id));
        }else {
            mAdapter.refreshDatas(MainData.getMainFragmentDatas(concertSelectEntity,false,id));
        }

    }

    @Override
    public void returnHomeEntityMore(List<ConcertSelectEntity.RecentLearnBean> list) {
        mAdapter.appendDatas(MainData.appendMainFragmentDatas(list,id));
        mBinding.refreshLayout.finishLoadmore();
        mBinding.refreshLayout.setLoadmoreFinished(false);
    }

    @Override
    public void returnHomeEntityNoMore() {
        mBinding.refreshLayout.finishLoadmore();
        mBinding.refreshLayout.setLoadmoreFinished(true);
    }

    @Override
    public void returnHomeEntityError(String msg) {
        mBinding.refreshLayout.setLoadmoreFinished(false);
        mBinding.refreshLayout.finishRefresh();
    }

}
